﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using AppManager.Model;
using System.Runtime.Remoting.Messaging;
using AppManager.DAL;
using System.Threading.Tasks;
using System.Transactions;

namespace AppManager.BLL
{
    public class BaseBLL<T> where T : BaseModel
    {
        //#region
        //public static async Task Add(T model)
        //{
        //    await BaseDAL<T>.Insert(model);
        //}

        //public static async Task Update(T model)
        //{
        //    await BaseDAL<T>.Update(model);
        //}

        //public static async Task AddOrUpdate(T model)
        //{
        //    await BaseDAL<T>.AddOrUpdate(model);
        //}

        //public static async Task UpdateIgnore(Expression<Func<T, object>> propertyExpression, params T[] entities)
        //{
        //    await BaseDAL<T>.UpdateIgnore(propertyExpression, entities);
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="originalEntity"></param>
        ///// <param name="newEntity">只能使用匿名对象</param>
        ///// <s></s>
        //public static async Task Update(T originalEntity, object newEntity)
        //{
        //    await BaseDAL<T>.Update(originalEntity, newEntity);
        //}

        //public static async Task Delete(T model)
        //{
        //    await BaseDAL<T>.Delete(model);
        //}

        //public static async Task Delete(Expression<Func<T, bool>> predicate)
        //{
        //    await BaseDAL<T>.Delete(predicate);
        //}

        //public static async Task AddList(IEnumerable<T> list)
        //{
        //    await BaseDAL<T>.InsertAll(list);
        //}

        //public static async Task UpdateAll(IEnumerable<T> list)
        //{
        //    await BaseDAL<T>.UpdateAll(list);
        //}

        //public static async Task DeleteList(IEnumerable<T> list)
        //{
        //    await BaseDAL<T>.DeleteList(list);
        //}

        //public static async Task DeleteList(Expression<Func<T, bool>> predicate)
        //{
        //    await BaseDAL<T>.DeleteList(predicate);
        //}

        //public static async Task<T> GetModel(Expression<Func<T, bool>> predicate = null)
        //{
        //    return await BaseDAL<T>.GetModel(predicate);
        //}
        //public static async Task<IEnumerable<T>> GetList(Expression<Func<T, bool>> predicate = null)
        //{
        //    return await BaseDAL<T>.GetList(predicate, false);
        //}

        //public static async Task<List<T>> GetListNoTrack(Expression<Func<T, bool>> predicate = null)
        //{
        //    //将数据实例化
        //    return (await BaseDAL<T>.GetList(predicate, true)).ToList();
        //}
        //#endregion

        //public static TransactionScope GetTransaction()
        //{
        //    TransactionOptions option = new TransactionOptions()
        //    {
        //        IsolationLevel = IsolationLevel.ReadCommitted,
        //        Timeout = TimeSpan.FromSeconds(60),
        //    };

        //    //使用該屬性表示嵌套事務中不創建新的事務，也就是在所有嵌套的方法中所有的操作在一個事務中
        //    return new TransactionScope(TransactionScopeOption.Required, option);
        //}

        public static Db Db
        {
            get { return BaseDAL<T>.db; }
        }
        public static void Add(T model)
        {
            BaseDAL<T>.Insert(model);
        }

        public static void Update(T model)
        {
            BaseDAL<T>.Update(model);
        }

        public static void AddOrUpdate(T model)
        {
            BaseDAL<T>.AddOrUpdate(model);
        }

        public static void UpdateIgnore(Expression<Func<T, object>> propertyExpression, params T[] entities)
        {
            BaseDAL<T>.UpdateIgnore(propertyExpression, entities);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="originalEntity"></param>
        /// <param name="newEntity">只能使用匿名对象</param>
        /// <s></s>
        public static void Update(T originalEntity, object newEntity)
        {
            BaseDAL<T>.Update(originalEntity, newEntity);
        }

        public static void Delete(T model)
        {
            BaseDAL<T>.Delete(model);
        }

        public static void Delete(Expression<Func<T, bool>> predicate)
        {
            BaseDAL<T>.Delete(predicate);
        }

        public static void AddList(IEnumerable<T> list)
        {
            BaseDAL<T>.InsertAll(list);
        }

        public static void UpdateAll(IEnumerable<T> list)
        {
            BaseDAL<T>.UpdateAll(list);
        }

        public static void DeleteList(IEnumerable<T> list)
        {
            BaseDAL<T>.DeleteList(list);
        }

        public static void DeleteList(Expression<Func<T, bool>> predicate)
        {
            BaseDAL<T>.DeleteList(predicate);
        }

        public static T GetModel(Expression<Func<T, bool>> predicate = null)
        {
            return BaseDAL<T>.GetModel(predicate);
        }
        public static IEnumerable<T> GetList(Expression<Func<T, bool>> predicate = null)
        {
            return BaseDAL<T>.GetList(predicate, false);
        }

        public static List<T> GetListNoTrack(Expression<Func<T, bool>> predicate = null)
        {
            //将数据实例化
            return (BaseDAL<T>.GetList(predicate, true)).ToList();
        }
    }
}
